/**
 * ValidateBox插件扩展 
 * 基于jQuery 1.8.0、EasyUI 1.3.2开发
 * 参照EasyUI 1.5.4.2的属性、事件与方法进行扩展
 * 向前兼容处理
 * @version 1.0
 * @author 范围兄 <ambit_tsai@qq.com>
 * @requires jQuery 1.8.0 or above
 * @requires EasyUI 1.3.2 or above
 * @see https://gitee.com/ambit/easyui_exts/blob/master/validatexbox.md
 */   
;(function($){
	'use strict';
	
	var plugin = 'validatebox';
	
	if(typeof $.fn[plugin]!=='function' || $.type($.fn[plugin].defaults)!=='object'){
		throw 'jQuery.fn.'+plugin+' is invalid';
	}
	

	// 代理插件解释器 
	var fn = $.fn[plugin];
	$.fn[plugin] = function(options, param){
		if(typeof options === 'string')
			return $.fn[plugin].__fn.call(this, options, param);

		return this.each(function(){
			var jq = $(this), 
				defs = $.fn[plugin].__fn.defaults,
				opts = $.extend(	// 解析得到配置选项
					{},
					$.fn[plugin].defaults,
					$.fn[plugin].parseOptions(this),
					options || {}
				), 
				onAfterCreate;

			if( !defs.hasOwnProperty('onBeforeCreate') ){
				opts.onBeforeCreate.call(jq);	// 触发onBeforeCreate事件
				delete opts.onBeforeCreate;	// 防止传递给其他扩展，导致重复触发 
			}
			if( !defs.hasOwnProperty('onAfterCreate') ){
				onAfterCreate = opts.onAfterCreate;
				delete opts.onAfterCreate;	// 防止传递给其他扩展，导致重复触发 
			}
			
			$.fn[plugin].__fn.call(jq, opts, param);
			opts = jq.data(plugin).options;	// 重新获取配置选项 

			// 解释扩展的属性与事件 
			if(!defs.hasOwnProperty('novalidate') && opts.novalidate){
				opts.novalidate = false;
				jq.validatebox('disableValidation');
			}
			if(!defs.hasOwnProperty('disabled') && opts.disabled){
				opts.disabled = false;
				jq.validatebox('disable');
			}
			if(!defs.hasOwnProperty('editable') && !opts.editable){
				jq.prop('readonly', true);
			}
			if(!defs.hasOwnProperty('readonly') && opts.readonly){
				jq.validatebox('readonly', true);
			}
			if(!defs.hasOwnProperty('validateOnCreate') && opts.validateOnCreate){
				jq.validatebox('validate');
			}
			if(!defs.hasOwnProperty('validateOnBlur') && opts.validateOnBlur){
				jq.on('blur.ambit_validatebox', function(){
					$(this).validatebox('validate');
				});
			}
			if(!defs.hasOwnProperty('autofocus') && opts.autofocus){
				jq.prop('autofocus', true).focus();
			}
			if(!defs.hasOwnProperty('placeholder') && opts.placeholder){
				jq.attr('placeholder', opts.placeholder);
			}
			if( onAfterCreate ){
				onAfterCreate.call(jq);	// 触发onAfterCreate事件
			}
		});
	};
	$.extend($.fn[plugin], fn);
	$.fn[plugin].__fn = fn;


	// 扩展属性与事件 
	$.fn[plugin].defaults = $.extend({
		novalidate: false,
		editable: true,
		disabled: false,
		readonly: false,
		validateOnCreate: true,
		validateOnBlur: false,
		autofocus: false,
		placeholder: '',
		onBeforeCreate: function(){},
		onAfterCreate: function(){}
	}, fn.defaults);


	// 扩展方法 
	$.fn[plugin].methods = $.extend({
		options: function(jq){
			return jq.data('validatebox').options;
		},
		enableValidation: function(jq){
			return jq.each(function(){
				var state = $.data(this, 'validatebox'), 
					opts = state.options;
				if(!opts.novalidate) return;
				opts.required = state.__required;
				opts.validType = state.__validType;
				opts.novalidate = false;
			}).validatebox('validate');
		},
		disableValidation: function(jq){
			return jq.each(function(){
				var state = $.data(this, 'validatebox'), 
					opts = state.options;
				if(opts.novalidate) return;
				state.__required = opts.required;
				state.__validType = opts.validType;
				opts.required = false;
				opts.validType = null;
				opts.novalidate = true;
			}).validatebox('validate');
		},
		enable: function(jq){
			return jq.each(function(){
				var opts = $.data(this, 'validatebox').options;
				if(!opts.disabled) return;
				$(this).validatebox('enableValidation').prop('disabled', false);
				opts.disabled = false;
			});
		},
		disable: function(jq){
			return jq.each(function(){
				var opts = $.data(this, 'validatebox').options;
				if(opts.disabled) return;
				$(this).validatebox('disableValidation').prop('disabled', true);
				opts.disabled = true;
			});
		},
		readonly: function(jq, mode){
			return jq.each(function(){
				var opts = $.data(this, 'validatebox').options;
				mode = mode===undefined? true: !!mode;
				if(opts.editable) $(this).prop('readonly', mode);
				opts.readonly = mode;
			});
		}
	}, fn.methods);
	
}(jQuery));